import { message } from "antd";

/**
 * 消息提示器单例封装
 *
 */
class SingletonMessage {
  messageInstance = null;

  static getInstance() {
    if (SingletonMessage.instance === null) {
      SingletonMessage.instance = new SingletonMessage();
    }
    return SingletonMessage.instance;
  }

  constructor() {
    const resetMessage = (options) => {
      if (this.messageInstance) {
        return message.destroy(this.messageInstance);
      }
      options.onClose = () => {
        this.messageInstance = null;
      };
      this.messageInstance = message.open(options);
    };

    ["success", "error", "info", "warning", "loading"].forEach((type) => {
      message[type] = (message, duration = 3) => {
        const options = { type, duration };
        if (typeof message === "string") {
          options.content = message;
        }
        return resetMessage(options);
      };
    });

    return message;
  }
}

export default SingletonMessage = new SingletonMessage();
